#include "game.h"

Game::Game(const string& level, WINDOW* window) : w_(window), score_(0) {
  // Loading the map
  map_ = new Map(level);

  // Creating a player
  p_ = new Player(';', 1, 1);
}

Game::~Game() {
  if (p_ != NULL) delete p_;
  if (map_ != NULL) delete map_;
}

void Game::updateDisplay() {
  // Clear the display
  wclear(w_);

  // Redraw the game elements
  map_->display(w_);
  p_->display(w_);

  // Get rid of the cursor
  curs_set(CURSOR_INVISIBLE);

  // Commit the changes
  wrefresh(w_);
}

int Game::play() {
  while (true) {
    // Block, waiting for the next character
    char c = getch();
    if  (c == 'q' || c == 'Q') break;

    // Let the Player react to the new input
    p_->handleInput(c, map_->width(), map_->height(), map_);
    if (map_->inCollision(p_)) p_->undoLastMove();

    // Redraw the ouput
    updateDisplay();
  }

  return score_;
}
